package com.wee.aircoach_user;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.bigtotoro.util.SharedPreferencesUtil;
import com.wee.entity.MD5Util;

/**
 * Created by Solomon on 2015/11/24.
 */
public class RouleiActivity extends Activity{
    private WebView webView;

    private AlertDialog alertDialog;
    private ProgressDialog progressBar;
    private TextView title;
    private String weight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yinshi);
        //加载WebView
        weight= getIntent().getStringExtra("weight");
        initWebView();
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if(progressBar.isShowing()){
                progressBar.dismiss();
                webView.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            Toast.makeText(RouleiActivity.this, "网页加载出错！", Toast.LENGTH_LONG);

            alertDialog.setTitle("ERROR");
            alertDialog.setMessage(description);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }
            });
            alertDialog.show();
        }
    }

    protected void initWebView(){
        //设计进度条
        progressBar = ProgressDialog.show(RouleiActivity.this, null, "加载中…");
        //获得WebView组件
        webView = (WebView) this.findViewById(R.id.webview);
        title=(TextView)findViewById(R.id.title);
        webView.getSettings().setJavaScriptEnabled(true);

        /**地址*/
   //  String url = Constant.API_ACTION_INFO+"?id="+1;
        try {
             int exact=  SharedPreferencesUtil.getInt(this, Constant.USERTIME);
            webView.loadUrl(Constant.webview2+ MD5Util.md5(exact)+"&type=protein&weight="+weight);
        }catch ( Exception r){
            Toast.makeText(this,"您的网络可能出现问题",Toast.LENGTH_SHORT).show();
        }



        alertDialog = new AlertDialog.Builder(this).create();
        //设置视图客户端
        webView.setWebViewClient(new MyWebViewClient());
    }
}